/**
 * LoginController
 *
 * @description :: Server-side logic for managing logins
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {

  login: function (req, res) {
    var adminName = req.body.name;
    var passwd = req.body.password;
    Admin.find({name: adminName}).then(function (result) {
      console.log(result);
      if (result[0].passwd == passwd) {
        req.session.userId = 'admin';
        res.redirect('/admin');
      } else {
        return res.view('admin/login', {data: 'invalid password', layout: 'admin/layout_login'});
      }
    }).catch(function (err) {
      return res.serverError(err);
    })
  },

  logout: function (req, res) {
    if (req.session.userId == 'admin') {
      req.session.userId = void(0);
      res.redirect('/admin/login');
    }
  }
};

